/**
 * Copyright 2012 ibiblio
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class UrlMappings {
    static mappings = {
        "/item/$action?/$title?/$id" {
            controller = "item"
            //action = "show"
            constraints {
                id(matches:/[0-9]+/)
            }
        }
        "/torrent/show/$name?/$id" {
            controller = "torrent"
            action = "show"
            constraints {
                id(matches:/[0-9a-f]{40}/)
            }
        }

        //"/torrent/download/$id/$filename.$fileext" {
        "/torrent/download/$id/$filename" {
            controller = "torrent"
            action = "download"
            constraints {
                id(matches:/[0-9a-f]{40}/)
                //fileext(matches:/torrent/)
            }
        }

        /**
         * Legacy torrent.ibiblio.org item urls
         *
         * supported urls:
         *     /doc/12345 -> /item/show
         *     /doc/12345/torrents -> /item/downloads
         *     /doc/my-title/12345 -> /item/show
         *     /doc/my-title/12345/torrents -> /item/downloads
         *     /torrents/0123456789abcdef0123456789abcdef01234567 -> /torrent/show
         *     /torrents/download/0123456789abcdef0123456789abcdef01234567 -> /torrent/show (not download)
         */
        "/doc/$title?/$id/$torrents?" {
            controller = "item"
            action = "legacy_redirect"
            constraints {
                id(matches:/[0-9]+/)
                torrents(matches:/torrents/)
            }
        }
        "/doc/$id/$torrents?" {
            controller = "item"
            action = "legacy_redirect"
            constraints {
                id(matches:/[0-9]+/)
                torrents(matches:/torrents/)
            }
        }
        "/torrents/download/$id" {
            controller = "torrent"
            action = "legacy_redirect"
            download = true
            constraints {
                id(matches:/[0-9a-f]{40}/)
            }
        }
        "/torrents/$id" {
            controller = "torrent"
            action = "legacy_redirect"
            download = false
            constraints {
                id(matches:/[0-9a-f]{40}/)
            }
        }

        /**
         * open id mappings
         */
        "/login/openIdCreateAccount" {
            controller = 'openId'
            action = 'createAccount'
        }

        /**
         * Search and browse by collection
         */
        name browseCollection: "/browse/collection/$name/$id" {
            controller = "collection"
            action = "browse"
            constraints {
                id(matches:/[0-9]+/)
            }
        }
        name searchCollection: "/search/collection/$name/$id" {
            controller = "collection"
            action = "search"
            constraints {
                id(matches:/[0-9]+/)
            }
        }

        /**
         * Force search url to only the list action
         */
        "/search" {
            controller = 'search'
            action = 'list'
        }

        /**
         * REST API urls
         */
        "/api/item/$id"(controller:"itemApi") {
            action = [GET:"show", PUT:"update", POST:"save"]
        }
        "/api/torrent/$id"(controller:"torrentApi") {
            action = [GET:"show", DELETE:"delete", POST:"add"]
        }

        // Spring security remappings
        "/admin/acl/class/$action?/$id?" { controller = 'aclClass' }
        "/admin/acl/entry/$action?/$id?" { controller = 'aclEntry' }
        "/admin/acl/object_identity/$action?/$id?" { controller = 'aclObjectIdentity' }
        "/admin/acl/sid/$action?/$id?" { controller = 'aclSid' }
        "/admin/persistent_login/$action?/$id?" { controller = 'persistentLogin' }
        "/admin/user/$action?/$id?" { controller = 'user' }
        "/admin/role/$action?/$id?" { controller = 'role' }
        "/admin/requestmap/$action?/$id?" { controller = 'requestmap' }
        "/admin/securityInfo/$action?/$id?" { controller = 'securityInfo' }
        "/register/user/$action?/$id?" { controller = 'register' }
        "/register/code/$action?/$id?" { controller = 'registrationCode' }

        /**
         * Generic mappings
         */
        name generic: "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(view:"/index")
        "/grails/"(view:"/grails/index")
        "500"(view:'/errors/error_500')
        "404"(view:'/errors/error_404')
    }
}
